package com.simga.library.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.simga.library.R;
import com.simga.library.base.IPresenter;
import com.simga.library.base.IView;
import com.trello.rxlifecycle2.components.support.RxFragment;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import butterknife.ButterKnife;

/**
 * PackageName : com.doupaitv.texaspoker.base
 * Author : Simga
 * Date : 2018/07/12
 * Time : 10:24
 */
public abstract class MBaseFragment<T extends IPresenter> extends RxFragment {

    protected T presenter;
    protected MBaseActivity mContext;
    protected View mRootView;
    protected boolean mIsCreated;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = (MBaseActivity) getActivity();
        presenter = createPresenter();
        if (this instanceof IView) {
            if (presenter != null) {
                presenter.bindView((IView) this);
            }
        }
    }

    private T createPresenter() {
        Type genType = this.getClass().getGenericSuperclass();
        if (genType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) genType;
            Type[] actualTypeArguments = type.getActualTypeArguments();
            Class<T> tClass = (Class<T>) actualTypeArguments[0];
            if (tClass != IPresenter.class) {
                try {
                    return tClass.newInstance();
                } catch (java.lang.InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        return inflater.inflate(getViewId(), container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!mIsCreated) {
            firstInit(savedInstanceState);
        }
        init(savedInstanceState);
        initListener();
        mIsCreated = true;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public View getContentView() {
        return mRootView;
    }

    protected void finish() {
        mContext.finish();
    }

    protected void finishSimple() {
        mContext.finishSimple();
    }

    public void finishResult(Intent intene) {
        mContext.finishResult(intene);
    }

    public void finishResult() {
        mContext.finishResult();
    }

    public void startActivity(Bundle bundle, Class<?> target) {
        mContext.startActivity(bundle, target);
    }

    public void startForResult(Bundle bundle, int requestCode, Class<?> target) {
        Intent intent = new Intent(mContext, target);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        mContext.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }


    public void showLoading() {
        showLoading(true);
    }

    public void dismissLoading() {
        mContext.dismissLoading();
    }

    public void showLoading(final boolean cancelable) {
        mContext.showLoading(cancelable);
    }

    public void showToast(int res) {
        mContext.showToast(getString(res));
    }

    public void showToast(Object message) {
        mContext.showToast(message);
    }

    public void showErrorMsg(String code, String msg) {
        showToast(msg);
        if ("5000104".equals(code) || "5000103".equals(code)) {//未登录，失效
            return;
        }
    }

    protected abstract int getViewId();

    /**
     * 每次以replace方式切换fragment时都会调用，调用多次
     */
    protected abstract void init(Bundle savedInstanceState);

    /**
     * 只在第一次初始化fragment时调用，只会调用一次
     */
    protected void firstInit(Bundle savedInstanceState) {
    }

    protected void initListener() {
    }


}
